package com.zqwh.xtool.model.online;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.zqwh.yj.sdk.utils.httpRequest.RetrofitSingleton;
import com.zqwh.yj.sdk.utils.httpRequest.subscribers.NetCallback;
import com.zqwh.yj.sdk.utils.httpRequest.subscribers.ProgressSubscriber;
import com.zqwh.yj.sdk.utils.httpRequest.HttpResultFunc2;
import com.zqwh.xtool.api.offline.StockResposity;
import com.zqwh.xtool.api.online.StockApi;
import com.zqwh.xtool.config.BaseConfig;
import com.zqwh.xtool.domain.EpcPosition;
import com.zqwh.xtool.domain.EpcPositions;
import com.zqwh.xtool.domain.LendItem;
import com.zqwh.xtool.domain.StockVO;
import com.zqwh.xtool.greendao.db.Stock;
import com.zqwh.xtool.model.StockModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.RequestBody;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import static com.zqwh.xtool.ToolsApp.gUser;

public class StockModelOnlineImpl implements StockModel {
    private StockApi stockApi;
    private StockResposity stockService;
    private Context context;

    public StockModelOnlineImpl(Context context) {
        this.context = context;
        this.stockApi = RetrofitSingleton.getInstance(BaseConfig.BASE_URL).create(StockApi.class);
        this.stockService = new StockResposity(this.context);
    }

    @Override
    public void getStock(Long devId, Boolean hasRFID, Boolean hasLent, NetCallback<List<LendItem>> callback) {
        stockApi.getStock("Bearer "+gUser.getToken(), devId, hasRFID, hasLent)
                .map(new HttpResultFunc2<List<LendItem>>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, false));
    }

    @Override
    public void initStock(Long devId, String epcs, String toolIds, NetCallback<Object> callback) {
        stockApi.initStock("Bearer "+gUser.getToken(), devId, epcs, toolIds)
                .map(new HttpResultFunc2<Object>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, false));
    }

    @Override
    public void getStockListByDevId(Long devId, NetCallback<HashMap<String, ArrayList<StockVO>>> callback) {
        stockApi.getStockListByDevId("Bearer "+gUser.getToken(), devId)
                .map(new HttpResultFunc2<HashMap<String, ArrayList<StockVO>>>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, false));
    }

    @Override
    public void getLentStockList(Long devId, String epcs, String toolIds, NetCallback<ArrayList<StockVO>> callback) {
        stockApi.getLentStockList("Bearer "+gUser.getToken(), devId, epcs, toolIds)
                .map(new HttpResultFunc2<ArrayList<StockVO>>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, false));
    }

    @Override
    public void getNoRfidStockListByDevId(Long devId, NetCallback<ArrayList<StockVO>> callback) {
        stockApi.getNoRfidStockListByDevId("Bearer "+gUser.getToken(), devId)
                .map(new HttpResultFunc2<ArrayList<StockVO>>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, false));
    }

    @Override
    public void incStock(Long devId, String incEpcs, String incToolIds, NetCallback<Object> callback) {
        stockApi.incStock("Bearer "+gUser.getToken(), devId, incEpcs, incToolIds)
                .map(new HttpResultFunc2<>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, false));
    }

    @Override
    public void redStock(Long devId, String stockIds, NetCallback<Object> callback) {
        stockApi.redStock("Bearer "+gUser.getToken(), devId, stockIds)
                .map(new HttpResultFunc2<>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, false));
    }

    @Override
    public void updateEpcPosition(Long devId, List<String> positions, NetCallback<Object> callback) {
        EpcPositions epcPositions = new EpcPositions();
        epcPositions.setDevId(devId);
        List<EpcPosition> epcPositionList = new ArrayList<>();
        if (positions != null){
            for (String position : positions){
                EpcPosition epcPosition = new EpcPosition();
                epcPosition.setEpc(position.split("#")[0]);
                epcPosition.setPosition(Integer.valueOf(position.split("#")[1]));
                epcPositionList.add(epcPosition);
            }
        }
        epcPositions.setEpcPosition(epcPositionList);

        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").create();
        String obj = gson.toJson(epcPositions);

        RequestBody content = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),obj);
        stockApi.updateEpcPosition(content)
                .map(new HttpResultFunc2<>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, false));
    }

    @Override
    public boolean insertMultStock(List<Stock> stockList) {
        return stockService.insertMultStock(stockList);
    }
}
